package com.seal.commons.utils.template;

import java.io.*;
import java.util.Map;

/**
 *
 * 模板引擎抽象类
 * @author braveblackc
 * @since 2022/10/17 20:45
 */
public abstract class AbstractTemplateEngine implements TemplateEngine{


    @Override
    public void render(String name, Map<?, ?> bindingMap, OutputStream out) {
        this.render(name, bindingMap, new OutputStreamWriter(out));
    }

    @Override
    public void render(String name, Map<?, ?> bindingMap, File file) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            this.render(name, bindingMap, new OutputStreamWriter(fileOutputStream));
        }catch (IOException e){
            throw new RuntimeException("reder file error", e);
        }
    }

    @Override
    public String render(String name, Map<?, ?> bindingMap) {
        final StringWriter writer = new StringWriter();
        render(name, bindingMap, writer);
        return writer.toString();
    }
}
